package oop.ex3.crosswords;

/**
 * An implementation of CrosswordPostion and Comparable represent a slot position in the crossword
 * 
 * @author tsachiho
 * @author kobi_atiya
 * 
 */
public class MyCrosswordPosition implements CrosswordPosition, Comparable<MyCrosswordPosition> {

    private int     x;         // X coordinate
    private int     y;         // Y coordinate
    private boolean isVertical; // direction

    /**
     * Constuctor
     * 
     * @param x - x coordinate
     * @param y - y coordinate
     * @param isVertical - direction
     */
    public MyCrosswordPosition(int x, int y, boolean isVertical) {
        this.x = x;
        this.y = y;
        this.isVertical = isVertical;
    }

    @Override
    public int getX() {
        return x;
    }

    @Override
    public int getY() {
        return y;
    }

    @Override
    public boolean isVertical() {
        return isVertical;
    }

    @Override
    public String toString() {
        return "(" + x + "," + y + ") " + (isVertical ? "V" : "H");
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof CrosswordPosition)) return false;
        CrosswordPosition pos = (CrosswordPosition) obj;
        return (x == pos.getX() && y == pos.getY() && isVertical == pos.isVertical());
    }

    public int compareTo(MyCrosswordPosition other) {
        if (this.x == other.getX()) {
            if (this.y == other.getY()) {
                if (this.isVertical && !other.isVertical()) {
                    return 1;
                }
                if (this.equals(other)) return 0;
                return -1;
            }
            return Integer.signum(this.getY() - other.getY());
        }
        return Integer.signum(this.getX() - other.getX());
    }
}
